package com.ruoyi.deeplearn.controller;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.deeplearn.domain.DeepDataset;
import com.ruoyi.deeplearn.domain.DeepDatasetFile;
import com.ruoyi.deeplearn.domain.DeepModule;
import com.ruoyi.deeplearn.service.IDeepDatasetFileService;
import com.ruoyi.deeplearn.service.IDeepDatasetService;
import com.ruoyi.deeplearn.service.IDeepModuleService;
import com.ruoyi.framework.minio.MinioFile;
import com.ruoyi.framework.minio.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/dataset")
public class DatasetUploaderController {
    @Autowired
    private MinioService minioService;
    @Autowired
    private IDeepDatasetService deepDatasetService;
    @Autowired
    private IDeepModuleService deepModuleService;
    @Autowired
    private IDeepDatasetFileService deepDatasetFileService;


    @PostMapping("/upload/{datasetId}")
    public AjaxResult uploadFile(@PathVariable("datasetId")Long datasetId,
                                 @RequestBody List<MinioFile> files) throws Exception
    {
        try
        {
            if(datasetId==null)return AjaxResult.error("数据集id不能为空");
            if(files==null||files.size()==0)return AjaxResult.error("请上传文件");
            DeepDataset deepDataset = deepDatasetService.selectDeepDatasetById(datasetId);
            if(deepDataset==null)return AjaxResult.error("数据集不存在");
            if(deepDataset.getModuleId()==null)return AjaxResult.error("数据集配置错误，bucket");
            DeepModule deepModule = deepModuleService.selectDeepModuleByModuleId(deepDataset.getModuleId());
            if(deepModule==null)return AjaxResult.error("数据集配置错误，bucket");
            files.stream().forEach(file->{
                DeepDatasetFile datasetFile = new DeepDatasetFile();
                datasetFile.setModuleId(deepDataset.getModuleId());
                datasetFile.setStatus(1);
                datasetFile.setUrl(file.getUrl());
                datasetFile.setFileName(file.getFileName());
                datasetFile.setFileExt(file.getFileExt());
                datasetFile.setFileSize(file.getFileSize());
                datasetFile.setBucket(file.getBucket());
                datasetFile.setObjectName(file.getObjectName());
                datasetFile.setDatasetId(datasetId);
                datasetFile.setMarkStatus(0);
                datasetFile.setWashStatus(0);
                datasetFile.setMarkNeedCount(1);
                deepDatasetFileService.insertDeepDatasetFile(datasetFile);
            });
            Long totalFiles = deepDatasetService.countDatasetFileCount(datasetId);
            deepDataset.setTotalCount(totalFiles);
            deepDataset.setImportStatus(1);
            deepDatasetService.updateDeepDataset(deepDataset);
            AjaxResult ajax = AjaxResult.success();
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}
